﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using AutoResumeSend.Entity;
namespace AutoResumeSend.DAC
{
    public class SqlDao
    {
       /// <summary>
       /// 添加发送简历邮件条目
       /// </summary>
       /// <param name="occurInfo"></param>
       /// <returns></returns>
        public int AddResumeSendInfo(WebSiteInfo occurInfo)
        {
            int rowsAffected;
            int ret = -1;
            try
            {
                SqlParameter[] parameters = {
					new SqlParameter("@occurTtitle", SqlDbType.VarChar,200),
					new SqlParameter("@occurContent", SqlDbType.VarChar,2000),
					new SqlParameter("@occurUrl", SqlDbType.VarChar,200),
					new SqlParameter("@publishDate", SqlDbType.DateTime),
					new SqlParameter("@companyName", SqlDbType.VarChar,200),
					new SqlParameter("@companyContent", SqlDbType.VarChar,2000),
					new SqlParameter("@companyAddress", SqlDbType.VarChar,200),
                    new SqlParameter("@companyEmail",SqlDbType.VarChar,50),
                    new SqlParameter("@webName",SqlDbType.VarChar,200),
                    new SqlParameter("@webUrl",SqlDbType.VarChar,200),
                    new SqlParameter("@webId",SqlDbType.Int,4),
                    new SqlParameter("@outResult",SqlDbType.Int,4)};
                parameters[0].Value = occurInfo.OccurTitle;
                parameters[1].Value = occurInfo.OccurContent;
                parameters[2].Value = occurInfo.OccurUrl;
                parameters[3].Value = DateTime.Now; //occurInfo.PublishTime;
                parameters[4].Value = occurInfo.CompanyName;
                parameters[5].Value =occurInfo.CompanyContent;
                parameters[6].Value = occurInfo.CompanyAddress;
                parameters[7].Value = occurInfo.CompanyEmail;
                parameters[8].Value = occurInfo.WebName;
                parameters[9].Value = occurInfo.WebLink;
                parameters[10].Value = occurInfo.WebId;
                parameters[11].Direction = ParameterDirection.Output;

                DbHelperSQL.RunProcedure("UP_tbResumeSend_ADD", parameters, out rowsAffected);
                ret = (int)parameters[11].Value;

                return ret;
            }
            catch (Exception ex)
            {
                throw new DataException("添加发送求职简历条目失败", ex);
            }

        }

        public DateTime GetLastSendTime(int webId)
        {
            int rowsAffected;
            DateTime dt = DateTime.MinValue;
            try
            {
                SqlParameter[] parameters = {
				
                    new SqlParameter("@webId",SqlDbType.Int,4),
                    new SqlParameter("@outLastTime",SqlDbType.DateTime,8)};
             
                parameters[0].Value = webId;
                parameters[1].Direction = ParameterDirection.Output;

                DbHelperSQL.RunProcedure("UP_tbResumeSendLastTime_Get", parameters, out rowsAffected);
                if (!DateTime.TryParse(parameters[11].Value.ToString(), out dt))
                    dt = DateTime.MinValue;

                return dt;
            
            }
            catch (Exception ex)
            {
                throw new DataException("添加发送求职简历条目失败", ex);
            }

        }

        public bool isExistEmail(string email)
        {
            string strSql = string.Format("select * from tbResumeSend where companyEmail='{0}'", email);

            try
            {
                DataSet ds = DbHelperSQL.Query(strSql);
                if (ds != null && ds.Tables != null && ds.Tables[0].Rows.Count > 0)
                    return true;

                return false;
            }
            catch (Exception ex)
            {
                throw new DataException("检查邮件是否存在失败", ex);
            }
        }
   
    }
}
